import { useNode } from '@craftjs/core';
import { Switch } from "antd";
import React from 'react';

/**
 * @class CSwitch
 *
 * 开关
 *
 */
export const CSwitch = ({ propKey = '', onChange, ...props }) => {
  const {
    actions: { setProp },
    propValue,
  } = useNode(node => {
    // console.log(node.data.props[propKey] || defaultValue);
    return { propValue: node.data.props[propKey] };
  });
  // props[propKey] = defaultValue
  // setProp(props => {
  //   props[propKey] = defaultValue
  // }, 100);
  // console.log(propValue,'propValue');
  return (
    <Switch
      value={propValue}
      defaultChecked
      onChange={checked => {
        setProp(props => {
          props[propKey] = onChange ? onChange(checked) : checked;
        }, 100);
      }}
      {...props}
    />
  );
};
